package com.db.aggregation;

import com.db.protocol.message.ReportMessage;
import com.db.protocol.message.ReportMessagePerExchange;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author nulls.narik
 */
public class ExchangeAggregation extends AbstractAggregation {

    private final Map<String, ReportMessagePerExchange> storage;

    public ExchangeAggregation() {
        storage = new HashMap<String, ReportMessagePerExchange>();
    }

    @Override
    protected void clear() {
        storage.clear();
    }

    @Override
    protected void add(ReportMessage message) {
        if (storage.containsKey(message.getExchangeID())) {
            storage.get(message.getExchangeID()).addQuantity(message.getQuantity());
        } else {
            storage.put(message.getExchangeID(), new ReportMessagePerExchange(message));
        }
    }

    @Override
    protected List<ReportMessage> getList() {
        return new LinkedList<ReportMessage>(storage.values());
    }
}
